import { message } from 'antd';
import {
  getDictList,
  getCorpList,
  getChildList,
  getBusiParam,
  getProList,
  getPageCorpList,
  saveBusiParam,
  insertRsgnRsn,
  insertProbType,
  insertSerType,
  updateDiss,
  updatePro,
  deleteRsgnRsn,
  deleteProbType,
  deleteCorpDict,
  getModRsnInfo,
  getModProInfo,
} from '@/services/dictionset';

export default {
  state: {
    treeData: [],
    data: {},
    dictData: {},
    selectedTreeKeys: [],
    comFlag: true,
    pageInfo: {},
    corpId: '',
    execCorpId: '',
    corpName: '',
    modalTitle: '修改离职原因',
    modalVisible: false, // 公用modal
    activeKey: '1',
    vno: undefined,

    modalList: [
      { code: '1', type: '离职原因', serType: 'RSGN_RSN' },
      { code: '2', type: '问题人员类型', serType: '' },
      { code: '3', type: '社保服务类型', serType: 'INS_SERVICE_TYPE' },
      { code: '4', type: '公积金服务类型', serType: 'ACCU_SERVICE_TYPE' },
      { code: '5', type: '档案服务类型', serType: 'RCRD_SERVICE_TYPE' },
    ],
    pageData: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    modalFieldsValue: {},
    probTypeId: '',
    rsgnRsnId: '',
  },
  effects: {
    // 获取字典
    *fetchDictList({ payload }, { call, put }) {
      const response = yield call(getDictList, payload);
      if (response.success) {
        yield put({
          type: 'getDictData',
          payload: response.data,
        });
      } else {
        message.warning(response.message || '服务器错误');
      }
    },
    *fetchCorpList({ payload, callback }, { call, put }) {
      const response = yield call(getCorpList, payload);
      if (response.success) {
        yield put({
          type: 'getTreeData',
          payload: response.data,
        });
        callback(response.data);
      } else {
        message.warning(response.message);
      }
    },
    *fetchChildList({ payload }, { call, put }) {
      const response = yield call(getChildList, payload);
      const { corpId } = payload;
      if (response.success) {
        yield put({
          type: 'getChildrenData',
          payload: { ...response, corpId },
        });
        return response.data;
      }
      return message.warning(response.message || '服务器错误');
    },
    *fetchBusiParam({ payload }, { call, put }) {
      const responseData = yield call(getBusiParam, payload);
      if (responseData.success) {
        yield put({
          type: 'getPageInfoData',
          payload: responseData.data,
        });
      } else {
        message.warning(responseData.message || '服务器错误');
      }
    },
    *saveParam({ payload }, { call, put, select }) {
      const response = yield call(saveBusiParam, payload);
      if (response.success) {
        const execCorpId = yield select(state => state.busiparamset.selectedTreeKeys[0]);
        const responseData = yield call(getBusiParam, { execCorpId });
        if (responseData.success) {
          yield put({
            type: 'getPageInfoData',
            payload: responseData.data,
          });
        } else {
          message.warning(responseData.message || '服务器错误');
        }
        message.success('保存成功！');
      } else {
        message.warning(response.message || '服务器错误');
      }
    },
    *fetchPageList({ payload }, { call, put }) {
      let response = '';
      const { activeKey } = payload;
      const inParam = { ...payload, activeKey: undefined };
      switch (Number(activeKey)) {
        case 1:
          response = yield call(getPageCorpList, inParam);
          break;
        case 2:
          response = yield call(getProList, inParam);
          break;
        case 3:
          response = yield call(getPageCorpList, inParam);
          break;
        case 4:
          response = yield call(getPageCorpList, inParam);
          break;
        case 5:
          response = yield call(getPageCorpList, inParam);
          break;
        default:
          break;
      }
      const {
        pagerArg: { currentPage, pageSize },
      } = payload;
      if (response.success) {
        yield put({
          type: 'getRsgnData',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(response.message || '服务器错误');
      }
    },

    *dissAdd({ payload, callback }, { call, put }) {
      const response = yield call(insertRsgnRsn, payload);
      if (response.success) {
        callback(true);
        yield put({
          type: 'updateModalData',
          payload: { modalVisible: false },
        });
        message.success('新增成功！');
      } else {
        message.warning(response.message || '服务器错误');
      }
    },
    *proAdd({ payload, callback }, { call, put }) {
      const response = yield call(insertProbType, payload);
      if (response.success) {
        callback(true);
        yield put({
          type: 'updateModalData',
          payload: { modalVisible: false },
        });
        message.success('新增成功！');
      } else {
        message.warning(response.message || '服务器错误');
      }
    },
    *serTypeAdd({ payload, callback }, { call, put }) {
      const response = yield call(insertSerType, payload);
      if (response.success) {
        callback(true);
        yield put({
          type: 'updateModalData',
          payload: { modalVisible: false },
        });
        message.success('新增成功！');
      } else {
        message.warning(response.message || '服务器错误');
      }
    },
    *dissUpdate({ payload, callback }, { call, put }) {
      const response = yield call(updateDiss, payload);
      if (response.success) {
        callback(true);
        yield put({
          type: 'updateModalData',
          payload: { modalVisible: false },
        });
        message.success('修改成功！');
      } else {
        message.warning(response.message || '服务器错误');
      }
    },
    *proUpdate({ payload, callback }, { call, put }) {
      const response = yield call(updatePro, payload);
      if (response.success) {
        callback(true);
        yield put({
          type: 'updateModalData',
          payload: { modalVisible: false },
        });
        message.success('修改成功！');
      } else {
        message.warning(response.message || '服务器错误');
      }
    },
    *delDiss({ payload, callback }, { call }) {
      const response = yield call(deleteRsgnRsn, payload);
      if (response.success) {
        callback(true);
        message.success('删除成功！');
      } else {
        message.warning(response.message || '服务器错误');
      }
    },
    *delPro({ payload, callback }, { call }) {
      const response = yield call(deleteProbType, payload);
      if (response.success) {
        callback(true);
        message.success('删除成功！');
      } else {
        message.warning(response.message || '服务器错误');
      }
    },
    *delCorpDictId({ payload, callback }, { call }) {
      const response = yield call(deleteCorpDict, payload);
      if (response.success) {
        callback(true);
        message.success('删除成功！');
      } else {
        message.warning(response.message || '服务器错误');
      }
    },

    *fetchModRsnInfo({ payload }, { call, put }) {
      const response = yield call(getModRsnInfo, payload);
      if (response.success) {
        const { rsgnRsnName, rsgnRsnCode, remark } = response.data;
        yield put({
          type: 'getModalData',
          payload: { modalFieldsValue: { rsgnRsnName, rsgnRsnCode, remark } },
        });
      } else {
        message.warning(response.message || '服务器错误');
      }
    },
    *fetchModProInfo({ payload }, { call, put }) {
      const response = yield call(getModProInfo, payload);
      if (response.success) {
        const { probTypeName, probTypeDesc } = response.data;
        yield put({
          type: 'getModalData',
          payload: { modalFieldsValue: { probTypeName, probTypeDesc } },
        });
      } else {
        message.warning(response.message || '服务器错误');
      }
    },
  },

  reducers: {
    // 获取字典数据
    getDictData(state, { payload }) {
      return {
        ...state,
        dictData: payload,
      };
    },
    // 获取树形数据
    getTreeData(state, { payload }) {
      const treeData = payload.map(item => ({
        title: item.companyName,
        key: item.corpId,
        corpName: item.companyName, // 区分父级和子级
        isLeaf: true,
      }));
      return {
        ...state,
        treeData,
        selectedTreeKeys: [payload[0].corpId],
        corpId: payload[0].corpId,
        execCorpId: payload[0].corpId,
        corpName: payload[0].companyName,
      };
    },
    getChildrenData(state, { payload }) {
      const childrenData = payload.data.map(item => ({
        title: item.name,
        key: `${item.orgId}`,
        dataInfo: item,
        name: item.name,
        isLeaf: payload.data.isLeaf || Number(item.functionType) === 1,
      }));
      const treeData = state.treeData.map(item => ({
        ...item,
        children: item.key === payload.corpId ? childrenData : item.children,
      }));
      return {
        ...state,
        treeData,
      };
    },
    updateModalData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    getPageInfoData(state, { payload }) {
      const pageInfo = {};
      Object.keys(payload).forEach(key => {
        if (key === 'autoDispch' || key === 'autoRcvr' || key === 'billGenRule') {
          pageInfo[key] = `${payload[key]}`;
        } else {
          pageInfo[key] = payload[key];
        }
      });
      return {
        ...state,
        pageInfo,
      };
    },
    getRsgnData(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        pageData: { list, pagination: { total: data.totalCount, current: currentPage, pageSize } },
      };
    },
    getModalData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
